<?php

namespace app\beautiful\controller\admin\partner;

use app\BaseController;

use app\beautiful\model\partner\PartnerSettingModel;



class Setting extends BaseController{

    public function show()

    {

        if(null === $partner_setting = PartnerSettingModel::where('shop_id',$this->request->shopId)->find()){

            $partner_setting=new PartnerSettingModel();

            $partner_setting->shop_id = $this->request->shopId;

            $partner_setting->dividend_ratio_data=[];

            $partner_setting->commission_data=[];

            $partner_setting->save();

        }



        $shareholder_partner_setting=[

            'dividend_ratio_data'=>$partner_setting->dividend_ratio_data,

            'is_bind_shareholder'=>$partner_setting->is_bind_shareholder,

        ];

        $employee_partner_setting=[

            'commission_data'=>$partner_setting->commission_data,

            'is_bind_employee'=>$partner_setting->is_bind_employee,

        ];

        $member_partner_setting=[

            'commission'=>$partner_setting->commission,

        ];

        $dividend_type_partner_setting=[

            'dividend_type'=>$partner_setting->dividend_type ?? 2,

        ];



        $return=[

            'shareholder_partner_setting'=>$shareholder_partner_setting,

            'employee_partner_setting'=>$employee_partner_setting,

            'member_partner_setting'=>$member_partner_setting,

            'dividend_type_partner_setting'=>$dividend_type_partner_setting,

        ];



        return $this->success($return);

    }



    public function save()

    {

        $type= $this->request->param('type/d',1);

        $dividend_ratio_data=$this->request->param('dividend_ratio_data','','security_json_or_array');

        $is_bind_shareholder=$this->request->param('is_bind_shareholder/d',0);

        //分红类型： 1按实付金额  2 按商品毛利
        $dividend_type=$this->request->param('dividend_type/d',2);

        $commission_data=$this->request->param('commission_data','','security_json_or_array');

        $is_bind_employee=$this->request->param('is_bind_employee/d',0);

        $commission=$this->request->param('commission/f',0);

        if(null === $partner_setting = PartnerSettingModel::where('shop_id',$this->request->shopId)->find()){

            $partner_setting=new PartnerSettingModel();

            $partner_setting->shop_id = $this->request->shopId;

        }



        //判断传来的参数

        if(!in_array($type,[1,2,3,4])) return $this->error('参数错误');

        //股份合伙人设置

        if($type==1){

            if(empty($dividend_ratio_data) || !is_array($dividend_ratio_data)) return $this->error('请填写股东合伙人股金设置');

            foreach ($dividend_ratio_data as $k => $v){

                if(empty($v['share_capital']) || $v['share_capital'] <=0) return $this->error('请输入股金设置第'.($k+1).'行的股金，且不能小于0');

                if($k!=0){

                    if($v['share_capital'] <= $dividend_ratio_data[$k-1]['share_capital']) return $this->error('股金设置第'.($k+1).'行的股金不能小于等于第'.$k.'行的股金');

                }

                if(empty($v['dividend_ratio']) || $v['dividend_ratio'] <0) return $this->error('请输入股金设置第'.($k+1).'行的分红比例，且不能小于0');

            }

            if(!in_array($is_bind_shareholder,[0,1])) return $this->error('请选择股金设置是否绑定已有用户');



            $partner_setting->dividend_ratio_data=$dividend_ratio_data;

            $partner_setting->is_bind_shareholder=$is_bind_shareholder;

        }



        //员工合伙人设置

        if($type==2){

            if(empty($commission_data) || !is_array($commission_data)) return $this->error('请填写员工合伙人业绩提成设置');

            foreach ($commission_data as $k => $v){

                if(empty($v['start_achievement']) || $v['start_achievement'] <=0) return $this->error('请输入员工合伙人业绩提成设置第'.($k+1).'行的最低月业绩，且不能小于0');

                if($k!=0){

                    if($v['start_achievement'] <= $commission_data[$k-1]['end_achievement']) return $this->error('员工合伙人业绩提成设置第'.($k+1).'行的最低月业绩不能小于等于第'.$k.'行的最高月业绩');

                }

                if(empty($v['end_achievement']) || $v['end_achievement'] <=0) return $this->error('请输入员工合伙人业绩提成设置第'.($k+1).'行的最高月业绩，且不能小于0');

                if($v['end_achievement'] <= $v['start_achievement']) return $this->error('员工合伙人业绩提成设置第'.($k+1).'行的最高月业绩不能小于等于最低月业绩');

                if(empty($v['commission']) || $v['commission'] <0) return $this->error('请输入员工合伙人业绩提成设置第'.($k+1).'行的提成比例，且不能小于0');

            }

            if(!in_array($is_bind_employee,[0,1])) return $this->error('请选择员工合伙人业绩提成设置是否绑定已有用户');



            $partner_setting->commission_data=$commission_data;

            $partner_setting->is_bind_employee=$is_bind_employee;

        }



        //员工合伙人设置

        if($type==3){

            if(empty($commission) || $commission <=0) return $this->error('请输入会员合伙人设置的佣金比例,且不能小于0');

            $partner_setting->commission=$commission;

        }

        //分红设置 $bonus_type : 1按实付金额  2 按商品毛利

        if($type==4){

            if(!in_array($dividend_type,[1,2])) return $this->error('请选择分红方式');

            $partner_setting->dividend_type=$dividend_type;

        }

        $partner_setting->save();

        return $this->success('操作成功');

    }

}

